<?php
$count_year = $_REQUEST['year'];
$count_month = $_REQUEST['month'];
$search_key = $count_year.$count_month;

//search_key因为下面的函数要用，这边搜索数据，只能重新组一个。
$count_day = $_REQUEST['days'];


if(($count_day)&&($count_day!="0"))
{
    $count_day=$count_day."日";
}
else{$count_day='';}



$order_date=$count_year."年".$count_month."月".$count_day;

if($search_key<1)
{$search_key=date('Y').date('m');}



//轮到传递过来部门的问题
$admin_dapartment = $_REQUEST['depart'];

if($admin_dapartment=="all")
{
    $deaprtment_str="";
}
else
{
    $deaprtment_str="and user_department = '".$admin_dapartment."'";
}


$sqlstr="select DISTINCT user_name,user_id from orderlist where order_date like '%".$order_date."%' and user_name !='' ".$deaprtment_str." order by user_id";
$result = mysql_query($sqlstr);



while ($row = mysql_fetch_array($result)) {
    $admin_user_name[] = $row['user_name'];
    $admin_user_id[] = $row['user_id'];
}

$sum=count($admin_user_name);








 

$nowpageid = 1; //当前页数
$detailnum = 20; // 


//翻页时候用到。如果有没有传递过来，打开第几页，就当第一页处理
if ($_REQUEST ['nowpageid']) {
    $nowpageid = $_REQUEST ['nowpageid'];
} else {
    $nowpageid = 1;
}


//翻页的时候，计算一下结果，应该从数据库的哪条条数据开始显示
$detailstar = ($nowpageid - 1) * $detailnum;


//$sums是搜索出来的结果，这里处理一下页面数.
if ($sum) //如果总数不等于0的话
{

    if ($sum < $detailnum) {
        $page_count = 1;
        $detailnum = $sum;
    } //如果总数据量小于$detailnum,detailnum作为每页显示数据的条数，）那么只有一页,而detailnum也更改为sum?


    else if ($sum % $detailnum) {
        $page_count = (int)($sum / $detailnum) + 1;
    }
    //取总数据量除以每页数的余数?
    //如果有余数，则页数等于总数据量除以每页数的结果取整再加一


    else {
        $page_count = $sum / $detailnum;
    }
    //如果没有余数，则页数等于总数据量除以每页数的结果?

} else //如果总数等于0的
{
    $page_count = 0;
    $detailnum = 0;
}


$lastpage = $page_count;
$prepage = $nowpageid - 1;
$nextpage = $nowpageid + 1;


//如果现在是第一页，上一页的页码被重置为0
if ($nowpageid == 1) {
    $prepage = 1;
}

//如果现在是最后一页
if($nowpageid==$page_count){

    if ($sum <= $detailnum) //数据的总条数少于每页显示数目，就把每页显示的数据（循环用）改成总数数目sum
    {
        $detailnum = $sum;
        $nextpage = $nowpageid;
    } else //现在是最后一页，并且总数数目大于循环数
    {
        $nextpage = $nowpageid; //重定义下一页为本页。因为是最后一页
        $detailnum = fmod($sum, $detailnum); //因为最后一页不一定是整好显示完(sum不一定能整除detailnum)，所以循环数是用余数的值
        if ($detailnum == 0) //这个if是因为当数据正好30条，能除尽的话，最后一页就会因为fmod能整除的缘故，最后一页不显示数据
        {
            $detailnum = 20;
        }
    }
    //如果现在页数等于总页数（总页数上面求出，最后一页不够循环数的时候），那么最后一页的detailnum就会等于余下数据(不足一页)的数目.

}
 
 



 

//取消翻页，所以所有数据都要走一遍
//for($_i=$detailstar;$_i<($detailstar+$detailnum);$_i++)
for($_i=0;$_i<$sum;$_i++)
{

    //查找部门和帐号
    $sqlstr="select * from user where
             user_name = '".$admin_user_name[$_i]."'";
    $result = mysql_query($sqlstr);


    while ($row = mysql_fetch_array($result)) {
        $admin_user_department[$_i] = $row['department'];
        $admin_user_account[$_i] = $row['account'];
    }



//按日计算早餐数。一日订单无论多少，只要订，就算1
    $sqlstr="select COUNT(distinct(order_date)) from orderlist where
             user_name = '".$admin_user_name[$_i]."' and
             order_at = '1' and
             order_date like '%".$order_date."%'";
    $query = mysql_query($sqlstr);

    $admin_user_zaocan_count[$_i] = mysql_result($query, 0); 
 
	if($admin_user_zaocan_count[$_i]==0)
	{$admin_user_zaocan_count[$_i]="";}



    if($admin_user_zaocan_count[$_i]){
    $sqlstr="select sum(product_price) from orderlist where
             user_name = '".$admin_user_name[$_i]."' and
             order_at = '1' and
             order_date like '%".$order_date."%'";
    $query = mysql_query($sqlstr);
    $admin_user_zaocan_cost[$_i] = mysql_result($query, 0);
    }


	
  
	//计算午餐，现在按照日期单选
	
	$sqlstr="select COUNT(distinct(order_date)) from orderlist where
             user_name = '".$admin_user_name[$_i]."' and
             order_at = '2' and
             order_date like '%".$order_date."%'";
    $query = mysql_query($sqlstr);

    $admin_user_wucan_count[$_i] = mysql_result($query, 0);
 
	if($admin_user_wucan_count[$_i]==0)
	{$admin_user_wucan_count[$_i]="";}


    if($admin_user_wucan_count[$_i]) {
        $sqlstr= "select sum(product_price) from orderlist where
             user_name = '" . $admin_user_name[ $_i ] . "' and
             order_at = '2' and
             order_date like '%".$order_date."%'";
        $query                         = mysql_query($sqlstr);
        $admin_user_wucan_cost[ $_i ] = mysql_result($query, 0);
    }

}







//晚餐只有一个选择，所以直接选择即可。但是要注意区分，点两餐是怎么算
$sqlstr="select COUNT(distinct(order_date)) from orderlist where
             user_name = '".$admin_user_name[$_i]."' and
             order_at = '3' and
             order_date like '%".$order_date."%'";
$query = mysql_query($sqlstr);

$admin_user_wancan_count[$_i] = mysql_result($query, 0);


if($admin_user_wancan_count[$_i]==0)
{$admin_user_wancan_count[$_i]="";}



if($admin_user_wancan_count[$_i]) {
    $sqlstr                        = "select sum(product_price) from orderlist where
             user_name = '" . $admin_user_name[ $_i ] . "' and
             order_at = '3' and
             order_date like '%".$order_date."%'";
    $query                         = mysql_query($sqlstr);
    $admin_user_wancan_cost[ $_i ] = mysql_result($query, 0);
}





//统计
 
 
 
    $sqlstr="select user_name,order_date from orderlist where
             order_at = '1' and
             order_date like '%".$order_date."%' and user_name !='' ".$deaprtment_str;






    $result = mysql_query($sqlstr);

    while ($row = mysql_fetch_array($result)) {
        $_1numbers[] = $row['user_name'].$row['order_date'];

    }
    $zaocan_num_totals = count(array_unique($_1numbers));
	
	
	
	

	
	    $sqlstr="select user_name,order_date from orderlist where
             order_at = '2' and
             order_date like '%".$order_date."%' and user_name !='' ".$deaprtment_str;
			 
    $result = mysql_query($sqlstr);

    while ($row = mysql_fetch_array($result)) {
        $_2numbers[] = $row['user_name'].$row['order_date'];

    }
    $wucan_num_totals = count(array_unique($_2numbers));
	
	
	
 
	
	
		
     $sqlstr="select user_name,order_date from orderlist where
             order_at = '3' and
             order_date like '%".$order_date."%' and user_name !='' ".$deaprtment_str;
			 
    $result = mysql_query($sqlstr);

    while ($row = mysql_fetch_array($result)) {
        $_3numbers[] = $row['user_name'].$row['order_date'];

    }
    $wancan_num_totals = count(array_unique($_3numbers));
	
	
	
	
	
	
	
	
	
 
        $sqlstr = "select sum(product_price) from orderlist where
             order_at = '1' and
             order_date like '%".$order_date."%' and user_name !='' ".$deaprtment_str;
        $query= mysql_query($sqlstr);
        $zaocan_cost_total = mysql_result($query, 0);
		
		
		$sqlstr = "select sum(product_price) from orderlist where
             order_at = '2' and
             order_date like '%".$order_date."%' and user_name !='' ".$deaprtment_str;
        $query= mysql_query($sqlstr);
        $wucan_cost_total = mysql_result($query, 0);
		
		
		$sqlstr = "select sum(product_price) from orderlist where
             order_at = '3' and
             order_date like '%".$order_date."%' and user_name !='' ".$deaprtment_str;
        $query= mysql_query($sqlstr);
        $wancan_cost_total = mysql_result($query, 0);
		
		
		
	

?>